import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

public class CalculatorApp extends JFrame {
    private JTextField textField;
    private JButton[] numberButtons;
    private JButton[] operatorButtons;
    private JButton equalsButton;
    private JButton clearButton;
    private JButton dotButton;

    private double num1, num2, result;
    private char operator;
    private boolean decimalMode = false;

    public CalculatorApp() {
        setTitle("Simple Calculator");
        setSize(300, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        // 初始化组件
        textField = new JTextField();
        textField.setFont(new Font("Arial", Font.PLAIN, 24));
        textField.setHorizontalAlignment(JTextField.RIGHT);
        textField.setPreferredSize(new Dimension(260, 40));

        numberButtons = new JButton[10];
        for (int i = 0; i < 10; i++) {
            numberButtons[i] = new JButton(String.valueOf(i));
            numberButtons[i].setPreferredSize(new Dimension(60, 60));
        }

        operatorButtons = new JButton[4];
        operatorButtons[0] = new JButton("+");
        operatorButtons[1] = new JButton("-");
        operatorButtons[2] = new JButton("*");
        operatorButtons[3] = new JButton("/");
        for (JButton button : operatorButtons) {
            button.setPreferredSize(new Dimension(60, 60));
        }

        equalsButton = new JButton("=");
        equalsButton.setPreferredSize(new Dimension(70, 70)); // 设置等号按钮大小

        clearButton = new JButton("C");
        clearButton.setPreferredSize(new Dimension(60, 60));

        dotButton = new JButton(".");
        dotButton.setPreferredSize(new Dimension(60, 60));

        // 添加事件监听器
        addListeners();

        // 使用GridBagConstraints设置输入栏占据一整行
        GridBagConstraints textFieldConstraints = new GridBagConstraints();
        textFieldConstraints.gridx = 0;
        textFieldConstraints.gridy = 0;
        textFieldConstraints.gridwidth = 4;
        textFieldConstraints.fill = GridBagConstraints.BOTH;
        textFieldConstraints.insets = new Insets(10, 10, 10, 10);
        add(textField, textFieldConstraints);

        // 添加数字按钮
        int gridX, gridY;
        for (int i = 1; i <= 9; i++) {
            gridX = (i - 1) % 3;
            gridY = (i - 1) / 3 + 1;
            GridBagConstraints buttonConstraints = new GridBagConstraints();
            buttonConstraints.gridx = gridX;
            buttonConstraints.gridy = gridY;
            buttonConstraints.fill = GridBagConstraints.BOTH;
            buttonConstraints.insets = new Insets(5, 5, 5, 5);
            add(numberButtons[i], buttonConstraints);
        }

        // 添加运算符按钮
        for (int i = 0; i < 4; i++) {
            gridX = 3;
            gridY = i + 1;
            GridBagConstraints operatorConstraints = new GridBagConstraints();
            operatorConstraints.gridx = gridX;
            operatorConstraints.gridy = gridY;
            operatorConstraints.fill = GridBagConstraints.BOTH;
            operatorConstraints.insets = new Insets(5, 5, 5, 5);
            add(operatorButtons[i], operatorConstraints);
        }

        // 设置小数点、清除按钮
        gridX = 0;
        gridY = 4;
        GridBagConstraints dotButtonConstraints = new GridBagConstraints();
        dotButtonConstraints.gridx = gridX;
        dotButtonConstraints.gridy = gridY;
        dotButtonConstraints.fill = GridBagConstraints.BOTH;
        dotButtonConstraints.insets = new Insets(5, 5, 5, 5);
        add(dotButton, dotButtonConstraints);

        gridX = 1;
        gridY = 4;
        GridBagConstraints clearButtonConstraints = new GridBagConstraints();
        clearButtonConstraints.gridx = gridX;
        clearButtonConstraints.gridy = gridY;
        clearButtonConstraints.fill = GridBagConstraints.BOTH;
        clearButtonConstraints.insets = new Insets(5, 5, 5, 5);
        add(clearButton, clearButtonConstraints);

        // 设置等号按钮
        gridX = 2;
        gridY = 4;
        GridBagConstraints equalsButtonConstraints = new GridBagConstraints();
        equalsButtonConstraints.gridx = gridX;
        equalsButtonConstraints.gridy = gridY;
        equalsButtonConstraints.gridwidth = 1;
        equalsButtonConstraints.fill = GridBagConstraints.BOTH;
        equalsButtonConstraints.insets = new Insets(5, 5, 5, 5);
        add(equalsButton, equalsButtonConstraints);

        // 设置窗口可见
        setVisible(true);
    }

    private void addListeners() {
        for (int i = 0; i < 10; i++) {
            final int number = i;
            numberButtons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    textField.setText(textField.getText() + number);
                }
            });
        }

        dotButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!decimalMode) {
                    textField.setText(textField.getText() + ".");
                    decimalMode = true;
                }
            }
        });

        for (int i = 0; i < 4; i++) {
            final char op;
            switch (i) {
                case 0:
                    op = '+';
                    break;
                case 1:
                    op = '-';
                    break;
                case 2:
                    op = '*';
                    break;
                case 3:
                    op = '/';
                    break;
                default:
                    op = ' ';
            }

            operatorButtons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!textField.getText().isEmpty()) {
                        num1 = Double.parseDouble(textField.getText());
                        operator = op;
                        textField.setText("");
                        decimalMode = false;
                    }
                }
            });
        }

        equalsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!textField.getText().isEmpty()) {
                    num2 = Double.parseDouble(textField.getText());
                    switch (operator) {
                        case '+':
                            result = num1 + num2;
                            break;
                        case '-':
                            result = num1 - num2;
                            break;
                        case '*':
                            result = num1 * num2;
                            break;
                        case '/':
                            if (num2 != 0) {
                                result = num1 / num2;
                            } else {
                                textField.setText("Error");
                                return;
                            }
                            break;
                        default:
                            return;
                    }
                    // 格式化结果为整数或小数
                    DecimalFormat decimalFormat = new DecimalFormat("#.###############");
                    textField.setText(decimalFormat.format(result));
                    decimalMode = false;
                }
            }
        });

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
                num1 = 0;
                num2 = 0;
                result = 0;
                operator = ' ';
                decimalMode = false;
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CalculatorApp();
            }
        });
    }
}
